//
//  XQOrderController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQOrderController: XQBaseController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {

    private var pageViewController: UIPageViewController!
    private var pages: [UIViewController] = []
    
    private lazy var menuView : XQOrderMenuView = {
        let view = XQOrderMenuView()
        view.menuHandle = {[weak self] index in
            guard let srongify = self else {return}
            let page = srongify.pages[index]
            srongify.pageViewController.setViewControllers([page], direction: .forward, animated: true, completion: nil)
        }
        return view
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        makeNav()
        makeUI()
        makeConstraints()
    }
    
    private func makeNav() {
        self.navTitle = "Order"
    }
    
    private func makeUI() {
        self.view.backgroundColor = .white
        self.view.addSubview(self.menuView)
        
        initPageController()
    }
    
    private func makeConstraints() {
        self.menuView.snp.makeConstraints { make in
            make.top.equalTo(UIDevice.xq_statusHeight() + 67)
            make.left.right.equalTo(0)
            make.height.equalTo(34)
        }
    }
    
    private func initPageController() {
        pageViewController = UIPageViewController(transitionStyle: .scroll,
                                                          navigationOrientation: .horizontal,
                                                          options: nil)
        pageViewController.dataSource = self
        pageViewController.delegate = self
        
        // 创建页面
        let firstPage = XQOrderPageController()
        firstPage.orderPageId = "7"
        let secondPage = XQOrderPageController()
        secondPage.orderPageId = "6"
        let thirdPage = XQOrderPageController()
        thirdPage.orderPageId = "5"
        
        pages = [firstPage, secondPage, thirdPage]
        
        // 设置初始页面
        pageViewController.setViewControllers([firstPage],
                                               direction: .forward,
                                               animated: true,
                                               completion: nil)
        
        // 添加页面视图控制器的视图
        addChild(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.view.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.menuView.snp.bottom).offset(24)
            make.bottom.equalTo(-UIDevice.xq_bottomSafeMargin() - 48)
        }
        pageViewController.didMove(toParent: self)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
       guard let index = pages.firstIndex(of: viewController), index > 0 else {
           return nil
       }
       return pages[index - 1]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
       guard let index = pages.firstIndex(of: viewController), index < pages.count - 1 else {
           return nil
       }
       return pages[index + 1]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
       if completed, let currentViewController = pageViewController.viewControllers?.first,
          let index = pages.firstIndex(of: currentViewController) {
           self.menuView.selectIndex = index
       }
   }

}
